package com.bclz.util;

import java.lang.reflect.Field;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.HtmlUtils;



public class EntityUtil {
	
	public static <T> T escape(T entity) throws IllegalArgumentException, IllegalAccessException {
		Field[] fields=entity.getClass().getDeclaredFields();
		int len=fields.length;
		for(int i=0;i<len;i++) {
			Field f=fields[i];
			//设置这个属性可以访问
			f.setAccessible(true);
			Object val=f.get(entity);
			if(!Objects.equals("content", f.getName())&&!Objects.equals("Post", entity.getClass().getName())) {
				if(f.getType()==String.class&&StringUtils.isNotEmpty((val==null?"":val).toString())) {
					f.set(entity, HtmlUtils.htmlEscape(val.toString()));
				}
			}
			
		}
		return entity;
	}
	
	
	
}
